﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO_DTO_WS.DAO;
using DAO_DTO_WS.DTO;

namespace LayoutTemplate
{
    public partial class CapNhatMatKhauaspx : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["TenDangNhap"].ToString() == "")
                    Response.Redirect("TrangLoi.aspx?Error=2");
            }
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            string tenDangNhap;
            try
            {
                tenDangNhap = Session["TenDangNhap"].ToString();
            }
            catch
            {
                Response.Redirect("DanhSachSanPham.aspx");
                return;
            }

            ThanhVienDTO tv = ThanhVienDAO.LayThanhVien(tenDangNhap);
            if (string.Compare(tv.MatKhau, tbMatKhauCu.Text, false) == 0)
                tv.MatKhau = tbMatKhauMoi.Text;
            else
            {
                Application["Back"] = Request.RawUrl;
                Response.Redirect("TrangLoi.aspx?Error=8");
            }
            ThanhVienDAO.SuaTV(tv);
            int loai =int.Parse(Session["TinhTrang"].ToString());
            if (loai == 2)
            {
                DoanhNghiepDTO dn = DoanhNghiepDAO.LayDNTheoTenDangNhap(tenDangNhap);
                dn.MatKhau = tv.MatKhau;
                DoanhNghiepDAO.SuaDN(dn);
            }
            Response.Redirect("ThanhCong.aspx?Success=2");
        }
    }
}
